package com.iw.adoms.dao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.iw.adoms.entity.Brand;
import com.iw.adoms.entity.Department;
import com.iw.adoms.entity.Marchandise;
import com.iw.core.orm.hibernate.BaseDao;

@Repository
public class MarchandiseDao extends BaseDao<Marchandise> {

	@Autowired
	private BrandDao brandDao;
	
	/**
	 * 查询当前部门的所有代理商品
	 * 
	 * @param departmentId
	 * @return
	 */
	public List<Marchandise> getAllMarchandiseByDepartMentId(Integer departmentId) {

		List<Marchandise> mAll = this.findAll();
		List<Marchandise> mList = new ArrayList<Marchandise>();
		List<Brand> allBrand = brandDao.findAll();
		Set<Brand> myBrands = new HashSet<Brand>();
		for (Brand b : allBrand) {
			Set<Department> departs = b.getDepartment();
			for (Department d : departs) {
				if (d.getId().equals(departmentId)) {
					myBrands.add(b);
				}
			}
		}
		// 只取代理的品牌下面的代理商品
		for (Marchandise m : mAll) {
			for (Brand b : myBrands) {
				if (b.getId().equals(m.getBrand().getId())) {
					if (!mList.contains(m)) {
						mList.add(m);
					}
				}
			}
		}

		return mList;

	}
	
}
